var OrderInfoConfigModel = function (data) {
  BaseModel.call(this);

  var self = this;
  var fn = {
    initData: function (data) {
      var orderInfoFieldObj = OrderInfoConfigModel.orderInfoFieldObj;
      var fields = data.fieldList;
      var fieldList = [];
      for (var i = 0; i < fields.length; i++) {
        var field = fields[i];
        var label = orderInfoFieldObj[field];
        fieldList.push({
          field: field,
          label: label
        });
      }
      self.fieldList = fieldList;

      self.widthList = data.widthList;
      self.colNum = data.colNum;
      self.rowNum = data.rowNum;
    }
  };

  this.getOrderLabel = function (rowIndex, colIndex) {
    var colNum = this.colNum;
    var index = rowIndex * colNum + colIndex;
    var fieldObj = this.fieldList[index];
    if (fieldObj === undefined) {
      return '';
    }
    return fieldObj.label;
  };

  this.getOrderFieldList = function () {
    var colNum = this.colNum;
    var rowNum = this.rowNum;
    var fieldList = this.fieldList;
    var fields = [];
    for (var i = 0; i < rowNum; i++) {
      var list = [];
      for (var j = 0; j < colNum; j++) {
        var obj = fieldList[i * colNum + j];
        if (obj === undefined) {
          list.push(null);
        } else {
          list.push(obj.field);
        }
      }
      fields.push(list);
    }
    return fields;
  };

  this.getAllOrderFields = function () {
    var fieldObj = OrderInfoConfigModel.orderInfoFieldObj;
    var allFields = [];
    for (var field in fieldObj) {
      var label = fieldObj[field];
      allFields.push({
        field: field,
        label: label
      });
    }
    return allFields;
  };

  this.resetFieldList = function (fieldList) {
    var orderInfoFieldObj = OrderInfoConfigModel.orderInfoFieldObj;
    var fieldList = _.map(fieldList, function (obj) {
      var field = obj.field;
      var label = orderInfoFieldObj[field];
      return {
        field: field,
        label: label
      };
    });
    this.fieldList = fieldList;
  };

  this.resetData = function (data) {
    var fieldList = data.fieldList;
    this.resetFieldList(fieldList);
    var colNum = data.colNum;
    if (this.colNum !== colNum) {
      var widthList = this.widthList;
      var widths = widthList.slice(0, widthList.length - 1);
      var colWiths = [];
      for (var i = 0; i < colNum; i++) {
        var labelWidth = widths[i * 2];
        var valueWidth = widths[i * 2 + 1];
        if (labelWidth === undefined) {
          labelWidth = widths[widths.length - 2];
          valueWidth = widths[widths.length - 1];
        }
        colWiths.push(labelWidth, valueWidth);
      }
      colWiths.push(widthList[widthList.length - 1]);
      this.widthList = colWiths;
    }
    this.rowNum = data.rowNum;
    this.colNum = colNum;
  }

  this.getObject = function () {
    var postData = {
      rowNum: this.rowNum,
      colNum: this.colNum,
      widthList: this.widthList
    };

    var fieldList = this.fieldList;
    fieldList = _.map(fieldList, 'field');
    postData.fieldList = fieldList;

    return postData;
  };

  fn.initData(data);
};

OrderInfoConfigModel.orderInfoFieldObj = {
  orderNo: "订单编号",
  sysOrderNo: "系统订单编号",
  factoryNo: "工厂型号",
  custmer: "客户",
  custNo: "客户编号",
  custmerVersionNo: "客户型号",
  custmerOrderNo: "客户订单号",
  orderSerialNo: "序列号",
  ringSize: "圈开料尺寸",
  legSize: "肶开料尺寸",
  approvalUser: "审核人",
  deliveryTime: "交货日期",
  orderTime: "下单日期",
  busNo: "业务编号",
  busUser: "业务员",
  frameType: "架型类别",
  proSize: "生产尺寸",
  printSize: "印字尺寸",
  punchLegSize: "冲腿尺寸",
  punchKnifeNo: "冲刀号",
  coreSize: "眼核尺寸",
  createUser: "制单人",
  orderTotal: "订单总数",
  feedTotal: "投料总数",
  orderType: "订单类型",
  qualityType: "品质",
  proDept: "生产部门"
};

OrderInfoConfigModel.orderMaterialFields = ['orderSerialNo', 'busNo', 'busUser', 'frameType', 'proSize', 'printSize', 'punchLegSize', 'punchKnifeNo',
  'coreSize', 'qualityType'
];
